from bs4 import BeautifulSoup,element
from bs4.element import Tag,NavigableString
import re

html = """
<html>
		Surface Reaper King=<div class="spawningMap" style="width:390px"><tabber>|-|
Aberration=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Aberration_Topographic_Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning Surface Reaper King Aberration.svg|link=|380px]]</div></div>|-|
Genesis: Part 1=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Genesis_Part_1_Topographic_Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning Surface Reaper King Genesis Part 1.svg|link=|380px]]</div></div>
</tabber>
</div>|-|
			Reaper Queen=<div class="spawningMap" style="width:390px"><tabber>|-|
Aberration=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Aberration_Topographic_Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning Reaper Queen Aberration.svg|link=|380px]]</div></div>
</tabber>
</div>|-|
			Corrupted Reaper King=<div class="spawningMap" style="width:390px"><tabber>|-|
Extinction=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Extinction_Topographic_Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning Corrupted Reaper King Extinction.svg|link=|380px]]</div></div>
</tabber>
</div>|-|
			R-Reaper King=<div class="spawningMap" style="width:390px"><tabber>|-|
Genesis: Part 2=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Genesis Part 2 Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning R-Reaper King Genesis Part 2.svg|link=|380px]]</div></div>
</tabber>
</div>|-|
			R-Reaper Queen=<div class="spawningMap" style="width:390px"><tabber>|-|
Genesis: Part 2=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Genesis Part 2 Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning R-Reaper Queen Genesis Part 2.svg|link=|380px]]</div></div>
</tabber>
</div>|-|
			Surface Reaper King Ghost=<div class="info-spawningmap-event">This variant spawns only during an [[Event]]</div><div class="spawningMap" style="width:390px"><tabber>|-|
Aberration=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Aberration_Topographic_Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning Surface Reaper King Ghost Aberration.svg|link=|380px]]</div></div>|-|
Genesis: Part 2=<div class="noviewer spawningMap-container" style="position: relative;width:380px;height:380px"><div style="position:absolute" class="spawningMap-map colorBlindToggleable">[[File:Genesis Part 2 Map.jpg|380px]]</div><div style="position:absolute;" class="svgCreatureMap colorBlindToggleable">[[File:Spawning Surface Reaper King Ghost Genesis Part 2.svg|link=|380px]]</div></div>
</tabber>
</div>
</html>
"""

def get_filename(text:str):
    match = re.match(r"^\[\[File:([^\|]*).*\]\]$",text)
    return match.group(1)

def format_habitats_name(name:str)->str:
    return name.replace("|-|","").replace("=","").strip()


soup = BeautifulSoup(html,"lxml")
habitats = []
for spawning_map in soup.select(".spawningMap"):
    habitat = {}
    element = spawning_map.previous_sibling
    # 判断是否为活动生物
    if type(element) is Tag and element.get("class") is not None:
        habitat["isEventCreature"] = True
        element = element.previous_sibling
    else:
        habitat["isEventCreature"] = False
    # 获取生物的名字
    if type(element) is Tag:
        species = element.text
    else:
        species = element.string
    # 对生物的名字进行格式化
    habitat["species"] = format_habitats_name(species)
    habitat["spawningData"] = []
    for map_container in spawning_map.select(".noviewer"):
        spawning_data = {}
        # 地图的名字
        map_name = map_container.previous_sibling
        spawning_data["mapName"] = format_habitats_name(map_name.string)
        # 地图的文件名
        div = map_container.find("div",class_="spawningMap-map")
        filename = get_filename(div.text)
        spawning_data["mapFile"] = filename
        
        # SVG分布图名
        div = map_container.find("div",class_="svgCreatureMap")
        filename = get_filename(div.text)
        spawning_data["svgFile"] = filename
        
        habitat["spawningData"].append(spawning_data)

    habitats.append(habitat)
    
